$(window).on('load', function () {
    $('.selectpicker').selectpicker({
        'selectedText': 'cat'
    });
});
$().ready(function() {
    validateRule();
    selCertificateList();
//省
    $('#adre').citys({province:"请选择"});
    //考点根据省份下拉
    $(".province1").change(function () {
        $("#orgId").empty();
        $("#expType").val("");
        $("#userId").val("");
        document.getElementById("userId").options.selectedIndex = 0; //回到初始状态
        $("#userId").selectpicker('refresh');//对下拉框进行重置刷新
        $.ajax({
            url: "/jzweb/personnelArrangement/selProTestCenter",
            data: {province:$("#province").val()},
            success: function (r) {
                for (var p in r.data) {
                    $("#orgId").append("<option value='" + r.data[p].id + "'>" + r.data[p].name + "</option>");
                }
            }
        });
    });
    //考点选择
    $("#orgId").change(function () {
        document.getElementById("userId").options.selectedIndex = 0; //回到初始状态
        $("#userId").selectpicker('refresh');//对下拉框进行重置刷新
        $("#expType").val("");
        $("#userId").val("");
    });
//专家信息下拉
    $("#expType").change(function () {
        $("#userId").empty();
        if($("#certificateList").val()==''){
            layer.msg("请先选择认证批次");
            return false
        }
        if($("#expType").val()==''){
            layer.msg("请先选择专家类型");
            return false
        }
        var cbatchId = $("#certificateList").val();
        var province =$("#province").val();
        var orgId =$("#orgId").val();
        var expType =$("#expType").val();
        var status =$('input[name="a"]:checked').val();
        var orgType =$('input[name="orgTypes"]:checked').val();
        //status0省内安排，1省外安排
            $.ajax({
                url: "/jzweb/personnelArrangement/selectExpert",
                data: {
                    cbatchId:cbatchId,
                    province:province,
                    orgId:orgId,
                    type:expType,
                    status:status,
                    orgType:orgType
                },
                success: function (r) {
                    $("#userId").append("<option value=''>请选择</option>");
                    for (var p in r.data) {
                        $("#userId").append("<option value='" + r.data[p].id + "'>" + r.data[p].name + "</option>");
                    }
                    $('.selectpicker').selectpicker('refresh');
                }
            });

    })
});

$.validator.setDefaults({
    submitHandler : function() {
        save();
    }
});

//认证批次下拉框
function selCertificateList() {
    $.ajax({
        url: "/jzweb/personnelArrangement/selCbatchSelect",
        success: function (r) {
            for (var p in r.data) {
                $("#certificateList").append("<option value='" + r.data[p].id + "'>" + r.data[p].name + "</option>");
            }
        }
    });
}

function save() {
    if($("#userId").val()==''){
        layer.msg("请选择考点人员");
        return false
    }
    $.ajax({
        cache : true,
        type : "POST",
        url : "/jzweb/personnelArrangement/save",
        data : $('#signupForm').serialize(),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}
function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules : {
            cbatchId : {
                required : true
            },
            province : {
                required : true
            },
            orgId : {
                required : true
            },
            expType : {
                required : true
            },
            userId : {
                required : true
            }
        },
        messages : {
        }
    })
}